package cn.cmkj.auction.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.Shuhua;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/8/27.
 */

public class MainShuAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<Shuhua> mData;
    private ImageOptions imageOptions;

    public MainShuAdapter(Context context, List<Shuhua> mData) {
        this.context = context;
        this.mData = mData;
        this.imageOptions = new ImageOptions.Builder()
                .setFadeIn(true).setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY)
                .build(); //淡入效果
    }

    @Override
    public int onSpan(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_main_shu, parent, false);
        return new MainShuViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Shuhua item = mData.get(position);
        if(item.getDefaultImage() != null) {
            x.image().bind(((MainShuViewHolder)holder).pic, item.getDefaultImage().m_url, imageOptions);
        }else {
            ((MainShuViewHolder)holder).pic.setImageResource(R.drawable.empty_photo);
        }
        ((MainShuViewHolder)holder).name.setText(item.getName());
        ((MainShuViewHolder)holder).salePrice.setText("￥" + item.getSalePrice());
        ((MainShuViewHolder)holder).oldPrice.setText("￥" + item.getOldPrice());
        ((MainShuViewHolder)holder).oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG ); //中间横线
        ((MainShuViewHolder)holder).readNum.setText("浏览量：" + item.getReadNum());
    }

    class MainShuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView pic;
        public TextView name;
        public TextView salePrice;
        public TextView oldPrice;
        public TextView readNum;

        public MainShuViewHolder(View itemView) {
            super(itemView);
            this.pic = (ImageView) itemView.findViewById (R.id.recommend_pic);
            this.name = (TextView) itemView.findViewById (R.id.recommend_name);
            this.salePrice = (TextView) itemView.findViewById (R.id.recommend_sale_price);
            this.oldPrice = (TextView) itemView.findViewById (R.id.recommend_old_price);
            this.readNum = (TextView) itemView.findViewById (R.id.recommend_read_num);
        }

        @Override
        public void onClick(View v) {

        }
    }
}
